home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13685 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  54 lines

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: making #define's to be seen across files
  5. Date: 9 Apr 1996 19:32:06 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4kedvm$fjp@sparcserver.lrz-muenchen.de>
  9. References: <1996Apr9.164443.28709@relay.nswc.navy.mil>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. dheffel@vitds2.nswc.navy.mil (David R. Heffelfinger) writes:
  13.  
  14. >Hi there,
  15. >    There is (hopefully) a simple answer to my question:
  16.  
  17. >I have some C code divided into different source files.
  18. >When I try to compile using 
  19. >  "cc file1.c file2.c file3.c"
  20. >the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?
  21.  
  22. Put those defines in a header file, and include the header file in 
  23. "file1.c", "file2.c" and "file3.c"? No, too simple :-). You could try
  24. to use _real_ constants:
  25.  
  26. Declare your "constants" in _one_ translation unit as
  27.  
  28.   const double PI = 3.141506;
  29.  
  30. Use
  31.  
  32.   extern const double PI;
  33.  
  34. in all other translation units. This has some advantages when working
  35. with a good debugger that _can_ inspect a const, but not a define.
  36.  
  37. The #define-directive is a preprocessor statement, and the proeprocessor
  38. performs textual exchange of a "#defined" macro with it's definition.
  39. The preprocessor does not create any constants that have any linkage 
  40. if you write 
  41.  
  42.    #define PI 3.141506
  43.  
  44. After preprocessing, the compiler does not distinguish between a
  45. line that contains "PI" and the same line that contains "3.141506",
  46. because both lines now contain "3.141506". "3.141506" is a floating
  47. point literal, and all you can do with it is use it in an expression. 
  48.  
  49. Kurt
  50. --
  51. | Kurt Watzka                             Phone : +49-89-2180-6254
  52. | watzka@stat.uni-muenchen.de
  53.  
  54.